DAY19:Beginner Series #3 Sum of Numbers


Posted by birdbirdmurmur on 2023-08-01

題目連結

Beginner Series #3 Sum of Numbers

解法

function getSum(a, b)
{
   const start = Math.min(a, b);
   const end = Math.max(a, b);
   let sum = 0;
   for (let i = start; i <= end; i++) {
     sum += i;
   }
   return sum;
}

心得

先找出最大和最小值
再利用迴圈一個一個加起來


#javascript #Codewars #Math.max #Math.min







Related Posts

RWD 設計原則

RWD 設計原則

連接RabbitMQ的AMQP時碰到The protocol requested by peer '0 0 9 1' does not match expected '3 1 0 0之原因與解法

連接RabbitMQ的AMQP時碰到The protocol requested by peer '0 0 9 1' does not match expected '3 1 0 0之原因與解法

CH3. 初探頭等函式 : 定義與引數

CH3. 初探頭等函式 : 定義與引數


Comments